﻿/***************************************************************************************
 *
 * 功能：工具条控件
 * 作者：waiting
 * 修改：wpwen
 * 记录：
 *      1、添加注释
 * 
 **************************************************************************************/

using System;
using System.Diagnostics;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;

namespace MeizuSDK.Presentation
{
    /// <summary>
    /// 工具条控件
    /// </summary>
    public class ButtonBar : Control
    {
        #region DllImport
        [DllImport("MZFCWrapper.dll")]
        private static extern IntPtr UiButtonBarCtor();

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiButtonBarCalcIndexOfPos(IntPtr pObj, int x, int y);
        
        [DllImport("MZFCWrapper.dll")]
        private static extern void UiButtonBarSetButtonCount(IntPtr pObj, int nCount);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiButtonBarGetButtonCount(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiButtonBarSetBarType(IntPtr pObj, int nType);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiButtonBarGetBarType(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiButtonBarSetDisabledTextColor(IntPtr pObj, uint Color);

        [DllImport("MZFCWrapper.dll")]
        private static extern uint UiButtonBarGetDisabledTextColor(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiButtonBarSetShowButton(IntPtr pObj, int nIndex, bool bShow);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool UiButtonBarIsButtonEnabled(IntPtr pObj, int nIndex);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool UiButtonBarIsButtonShow(IntPtr pObj, int nIndex);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiButtonBarSetButton(IntPtr pObj, int nIndex, bool bShow, bool bEnable, [MarshalAs(UnmanagedType.LPWStr)]string text);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiButtonBarSetButtonText(IntPtr pObj, int nIndex, [MarshalAs(UnmanagedType.LPWStr)]string text);
        
        [DllImport("MZFCWrapper.dll")]
        private static extern int UiButtonBarOnLButtonDown(IntPtr pObj, uint fwKeys, int xPos, int yPos);
    
        [DllImport("MZFCWrapper.dll")]
        private static extern int UiButtonBarOnLButtonUp(IntPtr pObj, uint fwKeys, int xPos, int yPos);

        [DllImport("MZFCWrapper.dll")]
        private static extern IntPtr UiButtonGetChildText(IntPtr pObj, int nIndex);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiButtonBarSetHighLightButton(IntPtr pObj, int nIndex);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiButtonBarGetHighLightButton(IntPtr pObj);
        
        #endregion

        /// <summary>
        /// 当用户点击工具条控件时触发此事件
        /// </summary>
        public event EventHandler<TabEventArgs> Click;
        /// <summary>
        /// 工具条控件高度
        /// </summary>
        public const int HEIGHT = MzMetrics.MZM_HEIGHT_BUTTONBAR;

        #region 私有变量
        private uint lastX, lastY;
        private int lastSelectedIndex;
        private bool mousedownSelected;
        List<ChildrenPair> collection;
        #endregion

        public class ChildrenPair
        {
            public int paraIndex;
            public Control childControl;

            public ChildrenPair(int paraIndex, Control childControl)
            {
                this.paraIndex = paraIndex;
                this.childControl = childControl;
            }
        }

        public ButtonBar()
        {
            Handle = UiButtonBarCtor();
            Location = new Point(0, Screen.ClientHeight - HEIGHT);
            Size = new Size(Screen.ClientWidth, HEIGHT);
            SetID();
            collection = new List<ChildrenPair>();
        }

        /// <summary>
        /// 添加控件
        /// </summary>
        /// <param name="paraIndex">选项卡索引</param>
        /// <param name="child">待添加的控件</param>
        public void Add(int paraIndex, Control child)
        {
            collection.Add(new ChildrenPair(paraIndex, child));
            //child.SetVisible(false);//默认不可见
            child.Visible = false;// 默认不可见
            if (UiButtonBarGetButtonCount(Handle) > 0)
            {
                UiButtonBarSetHighLightButton(Handle, 0);
                refreshMe(0);
            }
        }

        /// <summary>
        /// ButtonBar属性：按钮个数
        /// </summary>
        public int Count
        {
            get
            {
                return UiButtonBarGetButtonCount(Handle);
            }
            set
            {
                UiButtonBarSetButtonCount(Handle, (int)value);
            }
        }

        /// <summary>
        /// ButtonBar属性：按钮类型
        /// </summary>
        public int BarType
        {
            get
            {
                return UiButtonBarGetBarType(Handle);
            }
            set
            {
                UiButtonBarSetBarType(Handle, (int)value);
            }
        }
        /// <summary>
        /// 设置按钮文本
        /// </summary>
        /// <param name="nIndex">按钮索引</param>
        /// <param name="Text">按钮文本</param>
        public void SetButtonText(int nIndex, String Text)
        {
            UiButtonBarSetButtonText(Handle, nIndex, Text);
        }
        /// <summary>
        /// 设置按钮
        /// </summary>
        /// <param name="Index"></param>
        /// <param name="bShow"></param>
        /// <param name="bEnable"></param>
        /// <param name="text"></param>
        public void SetButton(int Index, bool bShow, bool bEnable, string text)
        {
            UiButtonBarSetButton(Handle, Index, bShow, bEnable, text);
        }

        private string getChildText(int childIndex)
        {
            //IntPtr p = UiButtonGetChildText(Handle, childIndex);
            //string s = Marshal.PtrToStringUni(p);
            return "";
        }

        /* 初始化刷新窗口 */
        private void refreshMe(int buttonBarIndex)
        {
            /* ---bengin刷新窗口显示控件 */
            ControlCollection controls = ((Form)Parent).Controls;
            
            int n = collection.Count;
            for (int i = 0; i < n; i++)
            {
                if (collection[i].paraIndex == buttonBarIndex)
                {
                    controls.Add(collection[i].childControl);
                    //collection[i].childControl.SetVisible(true);
                    collection[i].childControl.Visible = true;
                }
                else
                {
                    controls.Remove(collection[i].childControl);
                    //collection[i].childControl.SetVisible(false);
                    collection[i].childControl.Visible = false;
                }               
            }

            //((Form)Parent).Hide();
            //((Form)Parent).Show();
            ((Form)Parent).Invalidate();
            ((Form)Parent).Update();
            /* ---end刷新窗口显示控件 */
        }


        internal override void OnMzCommand(uint wParam, uint lParam)
        {
            if (Id != (wParam & 0xFFFF))
            {
                return;
            }           

            uint notify = (wParam & 0xFFFF0000) >> 16;
            int buttonBarIndex = (int)(lParam & 0xFFFF);
            int y = (int)(lParam & 0xFFFF0000);
            Debug.WriteLine("ButtonBar x:"+buttonBarIndex+"y:"+y);

            /* ---bengin刷新窗口显示控件 */
            refreshMe(buttonBarIndex);
            /* ---end刷新窗口显示控件 */

            if (Click != null)
            {
                Click(this, new TabEventArgs(buttonBarIndex, getChildText(buttonBarIndex)));
            }

            return;
        }


        //public void MzCommand(uint wParam, uint lParam)
        //{
        //    OnMzCommand(wParam, lParam);
        //}

        public class TabEventArgs : EventArgs
        {
            public TabEventArgs(int index, string text)
            {
                Index = index;
                Text = text;
            }

            public string Text
            {
                get;
                private set;
            }

            public int Index
            {
                get;
                private set;
            }
        }      
    }

    /// <summary>
    /// 按钮类型
    /// </summary>
    public enum ButtonBarType : int
    {
        /// <summary>
        /// 不绘制工具条背景
        /// </summary>
        None = -1,	
        /// <summary>
        /// 竖屏工具条：480 x 75
        /// </summary>
        Horizontal_480 = 0,
        /// <summary>
        /// 横屏工具条：720 x 50
        /// </summary>
        Vertical_720,
    }
}
